package com.hontech.bvapp2.ui.parts.heat

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Heat
import com.hontech.bvapp2.ui.dialog.runProgErr
import com.hontech.bvapp2.ui.view.LabelState
import com.hontech.bvapp2.ui.view.LabelSwitch
import com.hontech.bvapp2.ui.view.LabelTextView
import com.hontech.bvapp2.utils.registerEvent
import com.hontech.bvapp2.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class HeatStatusParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val lsPearlChecked: LabelState
    private val lsNozzleOrigin: LabelState
    private val lsStirTop: LabelState
    private val lsStirBottom: LabelState
    private val lsTrunLeft: LabelState
    private val lsTrunRight: LabelState
    private val lsClampLeft: LabelState
    private val lsClampRight: LabelState
    private val lsNozzleAlm: LabelState
    private val lsWaterHigh: LabelState
    private val lsWaterLow: LabelState
    private val lsTempFail: LabelState

    private val ltWaterTemp: LabelTextView
    private val ltSteamTemp: LabelTextView
    private val ltAdc: LabelTextView
    private val ltFlowCount: LabelTextView

    private val lswSteamHeat: LabelSwitch
    private val lswWaterHeat: LabelSwitch
    private val lswV3: LabelSwitch
    private val lswV4: LabelSwitch
    private val lswPump2: LabelSwitch
    private val lswPump1: LabelSwitch
    private val lswV6: LabelSwitch
    private val lswV7: LabelSwitch
    private val lswV2: LabelSwitch
    private val lswV1: LabelSwitch
    private val lswClampV: LabelSwitch
    private val lswTrunV: LabelSwitch
    private val lswWaterPump: LabelSwitch
    private val lswSteamPump: LabelSwitch
    private val lswV8: LabelSwitch
    private val lswAir: LabelSwitch

    private val ltDrawState: LabelTextView
    private val ltWaterHeatState: LabelTextView
    private val ltSteamHeatState: LabelTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_heat_status, this)

        lsPearlChecked = view.findViewById(R.id.id_heat_status_ls_pearl_checked)
        lsNozzleOrigin = view.findViewById(R.id.id_heat_status_ls_nozzle_origin)
        lsStirTop = view.findViewById(R.id.id_heat_status_ls_stir_top)
        lsStirBottom = view.findViewById(R.id.id_heat_status_ls_stir_bottom)
        lsTrunLeft = view.findViewById(R.id.id_heat_status_ls_trun_left)
        lsTrunRight = view.findViewById(R.id.id_heat_status_ls_trun_right)
        lsClampLeft = view.findViewById(R.id.id_heat_status_ls_clamp_left)
        lsClampRight = view.findViewById(R.id.id_heat_status_ls_clamp_right)
        lsNozzleAlm = view.findViewById(R.id.id_heat_status_ls_nozzle_alm)
        lsWaterHigh = view.findViewById(R.id.id_heat_status_ls_water_high)
        lsWaterLow = view.findViewById(R.id.id_heat_status_ls_water_low)
        lsTempFail = view.findViewById(R.id.id_heat_status_ls_temp_fail)

        ltWaterTemp = view.findViewById(R.id.id_heat_status_lt_water_temp)
        ltSteamTemp = view.findViewById(R.id.id_heat_status_lt_steam_temp)
        ltAdc = view.findViewById(R.id.id_heat_status_lt_adc)
        ltFlowCount = view.findViewById(R.id.id_heat_status_lt_flow_count)

        lswSteamHeat = view.findViewById(R.id.id_heat_status_lsw_steam_heat)
        lswWaterHeat = view.findViewById(R.id.id_heat_status_lsw_water_heat)
        lswV3 = view.findViewById(R.id.id_heat_status_lsw_v3)
        lswV4 = view.findViewById(R.id.id_heat_status_lsw_v4)
        lswPump2 = view.findViewById(R.id.id_heat_status_lsw_pump2)
        lswPump1 = view.findViewById(R.id.id_heat_status_lsw_pump1)
        lswV6 = view.findViewById(R.id.id_heat_status_lsw_v6)
        lswV7 = view.findViewById(R.id.id_heat_status_lsw_v7)
        lswV2 = view.findViewById(R.id.id_heat_status_lsw_v2)
        lswV1 = view.findViewById(R.id.id_heat_status_lsw_v1)
        lswClampV = view.findViewById(R.id.id_heat_status_lsw_clamp_v)
        lswTrunV = view.findViewById(R.id.id_heat_status_lsw_trun_v)

        lswWaterPump = view.findViewById(R.id.id_heat_status_lsw_water_pump)
        lswSteamPump = view.findViewById(R.id.id_heat_status_lsw_steam_pump)
        lswV8 = view.findViewById(R.id.id_heat_status_lsw_v8)
        lswAir = view.findViewById(R.id.id_heat_status_lsw_air)

        ltDrawState = view.findViewById(R.id.id_heat_status_lt_draw_state)
        ltWaterHeatState = view.findViewById(R.id.id_heat_status_lt_water_heat_state)
        ltSteamHeatState = view.findViewById(R.id.id_heat_status_lt_steam_heat_state)

        lswSteamHeat.onToggleFn = {
            ctrl("蒸汽加热器", 0, it)
        }
        lswWaterHeat.onToggleFn = {
            ctrl("热水加热器", 1, it)
        }
        lswV3.onToggleFn = {
            ctrl("v3", 2, it)
        }
        lswV4.onToggleFn = {
            ctrl("v4", 3, it)
        }
        lswPump2.onToggleFn = {
            ctrl("水泵2", 4, it)
        }
        lswPump1.onToggleFn = {
            ctrl("水泵1", 5, it)
        }
        lswV6.onToggleFn = {
            ctrl("v6", 6, it)
        }
        lswV7.onToggleFn = {
            ctrl("v7", 7, it)
        }
        lswV2.onToggleFn = {
            ctrl("v2", 8, it)
        }
        lswV1.onToggleFn = {
            ctrl("v1", 9, it)
        }
        lswClampV.onToggleFn = {
            ctrl("夹管阀", 10, it)
        }
        lswTrunV.onToggleFn = {
            ctrl("截流阀", 11, it)
        }
        lswWaterPump.onToggleFn = {
            ctrl("热水泵", 12, it)
        }
        lswSteamPump.onToggleFn = {
            ctrl("蒸汽泵", 13, it)
        }
        lswV8.onToggleFn = {
            ctrl("v8", 14, it)
        }
        lswAir.onToggleFn = {
            ctrl("气泵", 15, it)
        }
    }

    suspend fun ctrl(title: String, id: Int, v: Boolean) {
        val msg = if (v) "打开" else "关闭"
        runProgErr("${title}${msg}") {
            val ctrl = if (v) 0 else 1
            Heat.partsCtrl(id, ctrl)
        }
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onStatus(status: Heat.Status) {
        lsPearlChecked.set(status.pearlChecked())
        lsNozzleOrigin.set(status.nozzleOrigin())
        lsStirTop.set(status.stirTop())
        lsStirBottom.set(status.stirBottom())
        lsTrunLeft.set(status.trunLeft())
        lsTrunRight.set(status.trunRight())
        lsClampLeft.set(status.clampLeft())
        lsClampRight.set(status.clampRight())
        lsNozzleAlm.set(status.nozzleAlm())
        lsWaterHigh.set(status.waterHigh())
        lsWaterLow.set(status.waterLow())
        lsTempFail.set(status.tempFail())

        ltAdc.set(status.analog)
        ltFlowCount.set(status.flowCount)
        ltSteamTemp.set("${status.steamTemp}℃")
        ltWaterTemp.set("${status.waterTemp}℃")

        lswSteamHeat.set(status.steamHeat())
        lswWaterHeat.set(status.waterHeat())
        lswV3.set(status.v3())
        lswV4.set(status.v4())
        lswPump2.set(status.pump2())
        lswPump1.set(status.pump1())
        lswV6.set(status.v6())
        lswV7.set(status.v7())
        lswV2.set(status.v2())
        lswV1.set(status.v1())
        lswClampV.set(status.clampV())
        lswTrunV.set(status.trunV())
        lswWaterPump.set(status.waterPump())
        lswSteamPump.set(status.steamPump())
        lswV8.set(status.v8())
        lswAir.set(status.air())

        ltDrawState.set(status.drawState())
        ltWaterHeatState.set(status.waterHeatState())
        ltSteamHeatState.set(status.steamHeatState())
    }

}